package com.example.passwordmanager.dialog;

import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.example.passwordmanager.R;

public class EditTextDialogFragment extends BaseDialogFragment {
    private String title;
    private CallBack listener;

    public EditTextDialogFragment(String title, CallBack listener) {
        this.title = title;
        this.listener = listener;
    }

    @Override
    protected int getViewLayoutId() {
        return R.layout.dialog_edit_text;
    }

    private TextView tv_title, tv_cancel, tv_define;
    private EditText editText;

    @Override
    protected void initView(View root) {
        tv_title = root.findViewById(R.id.tv_title);
        tv_cancel = root.findViewById(R.id.tv_cancel);
        tv_define = root.findViewById(R.id.tv_define);
        editText = root.findViewById(R.id.edit_psd);
        tv_title.setText(title);
        tv_cancel.setOnClickListener(v -> {
            dismiss();
        });
        tv_define.setOnClickListener(v -> {
            String s = editText.getText().toString();
            listener.onClick(s);
            dismiss();
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();

        WindowManager m = getActivity().getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高度
        WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
//        p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6，根据实际情况调整
        p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.65，根据实际情况调整
        window.setAttributes(p);

    }

    public interface CallBack {
        void onClick(String s);
    }
}
